# coding=utf-8
"""
用户的model模块
"""

from sqlalchemy import Column, Integer, String, SmallInteger, BigInteger, ForeignKey, Boolean
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.sql.expression import text
from project.tools import JsonpickleMixin, Base, md5
from .base import BaseModel

class User(BaseModel, Base):
    """
    用户的model
    """
    __tablename__ = 'm_user'
    __table_args__ = {
        #'comment': '用户表'
    }
    # 用户名
    user_id = Column('user_id', String(50), nullable=False, doc='用户名',
            #comment='用户名'
        )
    # 姓名
    name = Column('name', String(200), nullable=False, doc='姓名',
            #comment='姓名'
        )
    # 密码
    password = Column('password', String(50), nullable=False, doc='密码',
            #comment='密码'
        )
    # 年龄
    age = Column('password', SmallInteger, nullable=False, doc='年龄',
            #comment='年龄'
        )
    # 性别
    gender = Column('gender', SmallInteger, nullable=False, doc='性别',
            #comment='性别'
        )
    # 电话
    phone = Column('phone', )
    is_deleted = Column('is_deleted', Boolean, server_default=text('FALSE'), doc='删除标志位: 0.未删除, 1.已删除',
            #comment='删除标志位: 0.未删除, 1.已删除'
        )

    @hybrid_method
    def set_encrypt_pwd(self, pwd):
        """
        密码加密
        """
        self.password = md5(pwd)

    def __getstate__(self):
        '''
        重写model的__getstate__
        删掉password
        :return:
        '''
        state = BaseModel.__getstate__(self)
        del state['password']
        return state
